package com.GLM.Viewer.Party;


import org.json.JSONArray;
import org.json.JSONObject;

import com.GLM.Constants.Constant;
import com.GLM.Tools.DB.DAO.UserInfoDAO;
import com.GLM.Tools.Utils.ViewsUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse;
import com.GLM.Tools.Utils.JSON.JSONUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse.Status;
import com.GLM.Viewer.R;
import com.google.gson.JsonObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CreateNewPartyViewActivity extends Activity {
	
	Animation mAnimIn;
	Animation mAnimOut;
	
	View mInputFieldOneLine;
	TextView mInputFieldOneLineTextView;
	EditText mInputFieldOneLineEditText;
	InputMethodManager imm;
	
	Button goBackButton;
	Button mStep1Button;
	Button mStep2Button;
	Button mStep3Button;

	String mPartyName;
	String mPartyDescription;
	String mBeginPartyTime;
	String mPartyPlaceName;
	String mPartyPlaceAddr;
	String mPartyPlacePhone;
	private int[] partyFriendsIds;
	private int meet_mg_id;
	private boolean tempStore = false;
	private boolean tempMap = false;
	private double[] lonlat;
	private String address;
	private String sourceActivity;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_party_createnew);
		
		initButtons();
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		initInputFields();
		initAnimation();
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		if(bundle != null){
			sourceActivity = bundle.getString("parentIntent");
			if(sourceActivity.equals("PartyStoreDetailActivity")){
				meet_mg_id = bundle.getInt("meet_mg_id");
				mPartyPlaceName = bundle.getString("meet_mg_name");
				mPartyPlaceAddr = bundle.getString("meet_mg_address");
				mPartyPlacePhone = bundle.getString("meet_mg_phone");
				mStep1Button.setText(ViewsUtil.getRegisterButtonSpannableString(mPartyPlaceName, mPartyPlaceAddr));
				tempStore = true;
			}
			else if(sourceActivity.equals("MapMainActivity")){
				address = bundle.getString(Constant.ADDRESS);
				if(address != null){
					lonlat = bundle.getDoubleArray(Constant.LOCATION);
					String strLine1 = getString(R.string.newparty_map_tip);
					mStep1Button.setText(ViewsUtil.getRegisterButtonSpannableString(strLine1, address));
					tempMap = true;
				}
				
			}
		}
		
		initRegisterInputFieldButton();
	}
	
	
	private void initButtons(){
		mStep1Button = (Button)findViewById(R.id.newpartybutton_step1);
		mStep2Button = (Button)findViewById(R.id.newpartybutton_step2);
		mStep3Button = (Button)findViewById(R.id.newpartybutton_step3);
		String step1_line1 = getString(R.string.newparty_unreg_step1_line1);
		String step1_line2 = getString(R.string.newparty_unreg_step1_line2);
		mStep1Button.setText(ViewsUtil.getRegisterButtonSpannableString(step1_line1, step1_line2));
		String step2_line1 = getString(R.string.newparty_unreg_step2_line1);
		String step2_line2 = getString(R.string.newparty_unreg_step2_line2);
		mStep2Button.setText(ViewsUtil.getRegisterButtonSpannableString(step2_line1, step2_line2));
		String step3_line1 = getString(R.string.newparty_unreg_step3_line1);
		String step3_line2 = getString(R.string.newparty_unreg_step3_line2);
		mStep3Button.setText(ViewsUtil.getRegisterButtonSpannableString(step3_line1, step3_line2));
		goBackButton = (Button)findViewById(R.id.goback_newparty_button);
		goBackButton.setOnClickListener(clickListeners);
		mStep1Button.setOnClickListener(clickListeners);
		mStep2Button.setOnClickListener(clickListeners);
		mStep3Button.setOnClickListener(clickListeners);
	}
	
	private void initInputFields() {
		mInputFieldOneLine = findViewById(R.id.inputfield_oneline);
		mInputFieldOneLine.setVisibility(View.GONE);
		mInputFieldOneLineTextView = (TextView) findViewById(R.id.inputfield_title);
		mInputFieldOneLineEditText = (EditText) findViewById(R.id.inputfield_edittext);

	}
	
	private void initAnimation() {
		mAnimIn = AnimationUtils.loadAnimation(this, R.anim.anim_register_in);
		mAnimOut = AnimationUtils.loadAnimation(this, R.anim.anim_register_out);
	}
	
	
	private void showOneLineInputField() {
		mInputFieldOneLine.startAnimation(mAnimIn);
		mInputFieldOneLine.setVisibility(View.VISIBLE);
		mInputFieldOneLineTextView.setText(getString(R.string.newparty_tips_createtext));
		mInputFieldOneLineEditText.requestFocus();
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
	}
	

	private void hideOneLineInputField() {
		mInputFieldOneLineEditText.setText("");
		mInputFieldOneLineEditText.setInputType(InputType.TYPE_NULL);
		mInputFieldOneLineEditText.clearFocus();
		mInputFieldOneLine.startAnimation(mAnimOut);
		mInputFieldOneLine.setVisibility(View.GONE);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
		
	}
	
	
	private void initRegisterInputFieldButton() {
		// one line
		Button inputCancelButton = (Button) findViewById(R.id.inputfield_cancelbutton);
		inputCancelButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				hideOneLineInputField();
			}
		});

		Button inputOkButton = (Button) findViewById(R.id.inputfield_okbutton);
		inputOkButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String meetName = mInputFieldOneLineEditText.getText().toString().trim();
				if(!meetName.equals("")){
					hideOneLineInputField();
					doCreateNewParty(meetName, mBeginPartyTime);
				}
			}
		});
		
		
	}
	
	public void doCreateNewParty(String meetName,String meetDate){
		JSONObject jsonData = new JSONObject();
		JSONResponse jsonResponse = null;
		try {
			jsonData.put(Constant.PARAM_USERID, UserInfoDAO.getUserInfo(CreateNewPartyViewActivity.this).getUserId());
			if(tempStore){
				jsonData.put("meet_mg_id", meet_mg_id);
			}
			else if(tempMap){
				jsonData.put("meet_lati",lonlat[0]);
				jsonData.put("meet_long", lonlat[1]);
			}
			jsonData.put("meet_name", meetName);
			jsonData.put("meet_beginDate", mBeginPartyTime);
			jsonData.put("meet_endDate", mBeginPartyTime);
			
			jsonData.put("meet_content", "");
			JSONArray ja = new JSONArray();
			for(int i=0; i<partyFriendsIds.length; i++){
				ja.put(partyFriendsIds[i]);
			}
			jsonData.put("enlist", ja);
			jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_CREATENEWPARTY, jsonData);
			if(jsonResponse.getStatus() != Status.ERR){
				JSONObject data = jsonResponse.getData();
				int status = -1;
				status = data.getInt(Constant.RESP_STATUS);
				if(status == 1){
					NavUtils.navigateUpTo(CreateNewPartyViewActivity.this, new Intent(CreateNewPartyViewActivity.this,PartyMainActivity.class));
					
					Toast.makeText(CreateNewPartyViewActivity.this, getString(R.string.newparty_create_success), Toast.LENGTH_LONG).show();
				}
				
						
			}
		} catch (Exception e) {
			// TODO: handle exception
			
			e.printStackTrace();
		}
	}
	
	
	OnClickListener clickListeners = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.newpartybutton_step1:
				newPartyStep1Button_OnClick();
				break;
			case R.id.newpartybutton_step2:
				newPartyStep2Button_OnClick();
				break;
			case R.id.newpartybutton_step3:
				newPartyStep3Button_OnClick();
				break;
			case R.id.goback_newparty_button:
				NavUtils.navigateUpTo(CreateNewPartyViewActivity.this, new Intent(CreateNewPartyViewActivity.this, PartyMainActivity.class));
				break;
			}
		}
	};
	
	private void newPartyStep1Button_OnClick(){
		Intent intent = new Intent(CreateNewPartyViewActivity.this,CreateNewPartyLocationActivity.class);
		startActivity(intent);
		
	}
	
	private void newPartyStep2Button_OnClick(){
		Intent intent = new Intent(CreateNewPartyViewActivity.this,CreateNewPartyFriendViewActivity.class);
		intent.putExtra("MeetingPlaceName", mPartyPlaceName);
		intent.putExtra("MeetingPlaceAddr", mPartyPlaceAddr);
		intent.putExtra("MeetingBegintTime", mBeginPartyTime);
		intent.putExtra("MeetingPlacePhone", mPartyPlacePhone);
		startActivityForResult(intent, Constant.CREATE_PARTY_FRIEND_CODE);
	}
	
	private void newPartyStep3Button_OnClick(){
		Intent intent = new Intent(CreateNewPartyViewActivity.this, CreateNewPartyTimeViewActivity.class);
		startActivityForResult(intent, Constant.CREATE_PARTY_TIME_CODE);
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		
		if(resultCode == RESULT_OK){
			switch (requestCode) {
			case Constant.CREATE_PARTY_TIME_CODE:
				mBeginPartyTime = data.getExtras().getString("mPartyBeginTime");
				mStep3Button.setText(ViewsUtil.getRegisterButtonSpannableString(mBeginPartyTime, getString(R.string.newparty_reg_step3_line1)));
				if((tempStore && partyFriendsIds != null) ||(tempMap && lonlat != null)){
					showOneLineInputField();
				}
				break;
			case Constant.CREATE_PARTY_FRIEND_CODE:
				Bundle bundle = data.getExtras();
				if(bundle != null){
					partyFriendsIds = bundle.getIntArray("chooseFriends");
					if(partyFriendsIds != null){
						int count = partyFriendsIds.length;
						String strLine1 = String.valueOf(count)+getString(R.string.person);
						String strLine2 = getString(R.string.newparty_tips_friend_choosed);
						mStep2Button.setText(ViewsUtil.getRegisterButtonSpannableString(strLine1, strLine2));
						for(int i=0; i<count; i++)
							Log.d("ids", partyFriendsIds[i]+"");
					}
				}
				
				break;
				
			default:
				break;
			}
		}
			
	}

	
}
